/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.validator;

import com.minierp.bean.OvBillBean;
import com.minierp.dao.OvBillDao;
import com.minierp.model.OvBill;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class OvBillValidator implements Validator {
	private OvBillDao ovBillDao;

	public OvBillDao getOvBillDao() {
		return this.ovBillDao;
	}

	public void setOvBillDao(OvBillDao ovBillDao) {
		this.ovBillDao = ovBillDao;
	}

	public boolean supports(Class clazz) {
		return OvBillBean.class.isAssignableFrom(clazz);
	}

	public void validate(Object obj, Errors errors) {
		OvBillBean bean = (OvBillBean) obj;
		OvBill form = bean.getOvBill();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		boolean hasError = false;
		if (form.getTallySetId() == 0L) {
			hasError = true;
			errors.rejectValue("ovBill.tallySetId", "", "请选择『当前开单帐套』");
		}

		if (!hasError
				&& (bean.getCodeRule().getGenerateRule().equals("MANUAL") || bean
						.getOldId() != 0L)) {
			if (form.getBillCode() != null
					&& !form.getBillCode().trim().equals("")) {
				OvBill e = this.ovBillDao.get(form.getBillCode());
				if (e != null && e.getId() != form.getId()) {
					errors.rejectValue("ovBill.billCode", "", "『编号』已经存在");
					hasError = true;
				}
			} else {
				errors.rejectValue("ovBill.billCode", "", "请输入『编号』");
				hasError = true;
			}
		}

		if (!hasError) {
			try {
				form.setOutDate(sf.parse(bean.getOutDateStr()));
			} catch (Exception arg8) {
				hasError = true;
				errors.rejectValue("outDateStr", "",
						"『日期』填写错误，请按以下格式输入：2008-01-01");
			}
		}

		if (!hasError) {
			if (form.getDepId() == 0L) {
				errors.rejectValue("ovBill.depId", "", "请选择『部门』");
				hasError = true;
			} else if (form.getOutType() != null
					&& !form.getOutType().equals("")) {
				if (form.getStafferId() == 0L) {
					errors.rejectValue("ovBill.stafferId", "", "请选择『经办人员』");
					hasError = true;
				} else {
					try {
						form.setCost(new BigDecimal(bean.getCostStr()));
					} catch (Exception arg7) {
						hasError = true;
						errors.rejectValue("costStr", "", "『产值金额』只能输入数字");
					}
				}
			} else {
				errors.rejectValue("ovBill.outType", "", "请选择『产值类型』");
				hasError = true;
			}
		}

		if (!hasError && form.getDescription() != null
				&& form.getDescription().length() >= 100) {
			hasError = true;
			errors.rejectValue("ovBill.description", "", "『摘要』不能超过100个字符");
		}

		if (!hasError && form.getMark() != null
				&& form.getMark().length() >= 100) {
			hasError = true;
			errors.rejectValue("ovBill.mark", "", "『备注』不能超过100个字符");
		}

	}
}